﻿namespace In66.Net.Repository.EFCoreData.MySql
{
    public static class EfCoreMySqlServiceCollectionExtension
    {
        private static readonly ConcurrentDictionary<string, char> s_RegisteredModels = new();

        public static bool HasRegistered(this IServiceCollection _, string modelName) => !s_RegisteredModels.TryAdd(modelName.ToLower(), '1');

        public static IServiceCollection AddInfraEfCoreMySql(this IServiceCollection services, Action<DbContextOptionsBuilder> optionsBuilder)
        {
            if (services.HasRegistered(nameof(AddInfraEfCoreMySql)))
            {
                return services;
            }

            services.AddDbContext<DbContext, MySqlDbContext>(optionsBuilder);
            services.TryAddScoped<IUnitOfWork, MySqlUnitOfWork<MySqlDbContext>>();
            services.TryAddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
            return services;
        }
    }
}
